#include "ThreadDialog.h"
#include "ui_ThreadDialog.h"
#include <QDebug>

ThreadDialog::ThreadDialog(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ThreadDialog)
{
    ui->setupUi(this);

    mThreadConsumer = new ThreadShow;
    mThreadProducer = new ThreadDAQ;

    connect(mThreadConsumer, &ThreadShow::newValue, this, &ThreadDialog::onThreadNewValue);
}

ThreadDialog::~ThreadDialog()
{
    if(mThreadConsumer){
        mThreadConsumer->deleteLater();
        mThreadConsumer = nullptr;
    }

    if(mThreadProducer){
        mThreadProducer->deleteLater();
        mThreadProducer = nullptr;
    }

    delete ui;
}

void ThreadDialog::onThreadNewValue(int *data, int count, int bufNo)
{
    //读取threadConsumer传递的缓冲区的数据
    QString str= QString("第%1个缓冲区: ").arg(bufNo) ;

    for (int i=0; i<count;i++)
    {
        str=str+QString("%1，").arg(*data);

        data++;
    }

    str=str+'\n';
    ui ->plainTextEdit->appendPlainText(str) ;
}


void ThreadDialog::on_pushButton_startThread_clicked()
{
    //启动线程
    mThreadConsumer->start();
    mThreadProducer->start();
}


void ThreadDialog::on_pushButton_endThread_clicked()
{
    //结束线程
 //   mThreadConsumer->stopThread();
    mThreadConsumer->terminate();
    mThreadConsumer->wait();

  //  mThreadProducer->stopThread();
    mThreadProducer->terminate();
    mThreadProducer->wait();

}

